package ming;

import ming.dao.UserDao;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LoginServlet extends HttpServlet {
    static int count=0;
    private static Map  NameMap = new HashMap();
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        ServletContext context = this.getServletContext();
        if(context.getAttribute("Online")==null)
            context.setAttribute("Online",0);

        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
//            response.getWriter().print(
//                    "您还没有登录，请<a href='/code_war_exploded/login'>登录</a>");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
            request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/main.jsp").forward(request, response);
        }

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        response.setContentType("text/html;charset=utf-8");
        System.out.println("new UserDao()");
        final UserDao userDao = new UserDao();
        List<User> userList = userDao.getUserInfo();


        String name = request.getParameter("name");
        String password = request.getParameter("password");
        boolean flag=false;
        for (User user:userList) {
            //说明用户存在
//            System.out.println("user.getUserName()"+user.getUserName());

            if( name.equals(user.getUserName()) ){
                System.out.println("user.getUserName()"+user.getUserName());

                ServletContext context = this.getServletContext();
//                context.setAttribute(user.getUserName(),"user.getUserName()");
//
//                Enumeration<String> paramNames = context.getAttributeNames();
//                System.out.println("paramNames.hasMoreElements()");
//                // 遍历所有的初始化参数名，得到相应的参数值并打印
//                while (paramNames.hasMoreElements()) {
//                    String Elementname = paramNames.nextElement();
//                    String value = (String)context.getAttribute(Elementname);
//                    System.out.println(Elementname + ":" +value);
//
//                }

                if(password.equals(user.getPassword())){
                    System.out.println("登录成功");
                    NameMap.put(request.getSession().getId(), request.getSession());
                    context.setAttribute("NameMap",NameMap);
                    context.setAttribute("Online",NameMap.size());
                    flag=true;
                    //放到session中,用来验证是否已经登录成功
                    request.getSession().setAttribute("flag","loginsucc");
//                    String info = (String) request.getSession().getServletContext().getAttribute("phone");
//                    if(info==null){
//                        request.getSession().getServletContext().setAttribute("phone",phone);
//                        //统计登录人数
//                        Integer logincount = (Integer)  request.getSession().getServletContext().getAttribute("logincount");
//                        request.getSession().getServletContext().setAttribute("logincount",logincount+1);
//                    }
//                    response.getWriter().write("yes");

                    request.setAttribute("result", "登录成功，欢迎"+name);

                    request.getSession().setAttribute("user", user);
                    request.getRequestDispatcher("/main.jsp").forward(request,response);
                    break;
                }else{
                    flag=true;
                    System.out.println("密码错误");
                    request.setAttribute("result", "登录密码错误"+password);
                    request.getRequestDispatcher("/login.jsp").forward(request,response);

                    response.getWriter().write("pwderr");
                    break;
                }
            }

        }


        if (flag==false)
        {System.out.println("用户不存在");
            request.setAttribute("result", "用户不存在");
            request.getRequestDispatcher("/login.jsp").forward(request,response);}
//        if(password.equals("123"))
//        {
//            System.out.println("登录成功");
//            User user = new User();
//            user.setUserName(name);
//            user.setPassword(password);
//
//            request.setAttribute("result", "登录成功，欢迎"+name);
//
//            request.getSession().setAttribute("user", user);
//            request.getRequestDispatcher("/main.jsp").forward(request,response);
//        }
//        else
//        {
//            System.out.println("登录失败");
//            request.setAttribute("result", "登录失败，密码"+pw);
//            request.getRequestDispatcher("/login.jsp").forward(request,response);
//        }

//        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }
}
